import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login'),
    meta: {
      public: true
      }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/register')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next) => {

  if (!to.meta.public && !localStorage.getItem('token')) {

    Vue.prototype.$message({
      type: 'warning',
      message: '没有权限登录'
    })
    return next('/login')
    
  }
  next()
})
export default router
